package com.telecomyt.jwportal.activity.login;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.ruffian.library.widget.RTextView;
import com.telecomyt.jwportal.R;
import com.telecomyt.jwportal.activity.login.bean.LoginBean;
import com.telecomyt.jwportal.base.BaseActivity;
import com.telecomyt.jwportal.constant.AppConstants;
import com.telecomyt.jwportal.utils.AppUtils;
import com.telecomyt.jwportal.utils.SPUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @date: 2020/8/25
 * @author: chengDa
 * @description: 登录页面
 */
public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.View {

    @BindView(R.id.mTvToLogin)
    RTextView mTvToLogin;
    @BindView(R.id.mTvCancleLogin)
    RTextView mTvCancleLogin;
    @BindView(R.id.mTvTitle)
    TextView mTvTitle;

    public static void start(Context context) {
        Intent starter = new Intent(context, LoginActivity.class);
        context.startActivity(starter);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        if (AppUtils.isLogin(this)) {
            refreshUI(true);
        } else {
            refreshUI(false);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected LoginPresenter creatPresenter() {
        return new LoginPresenter(this);
    }


    @OnClick({R.id.mTvToLogin, R.id.mTvCancleLogin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.mTvToLogin:
                login();
                break;
            case R.id.mTvCancleLogin:
                finish();
                break;
        }
    }

    /**
     * @date: 2020/8/25
     * @author: chengDa
     * @description: 未登录情况 5s倒计时结束去登录，  登录情况下-退出登录
     */
    private void login() {
        boolean isLogin = AppUtils.isLogin(this);
        if (isLogin) {
            persenter.loginOut();
        } else {
            persenter.toLogin("chengda100", "111111");
        }
    }

    /**
     * @date: 2020/8/25
     * @author: chengDa
     * @description: 登录/退出登录 刷新UI
     */
    private void refreshUI(boolean isLogin) {
        if (isLogin) {
            SPUtil.put(this, AppConstants.Login.SP_LOGINSTATUS, true);
            mTvTitle.setText("PC端已登录");
            mTvToLogin.setText("退出PC端登录");
            mTvCancleLogin.setVisibility(View.GONE);
        } else {
            SPUtil.clear(this);
            mTvTitle.setText("PC端未登录");
            mTvToLogin.setText("登录");
            mTvCancleLogin.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void loginSuccess(LoginBean bean) {
        refreshUI(true);

    }

    @Override
    public void loginOutSuccess() {
        refreshUI(false);
    }
}